**********************************************
************   Table 5:  PIECERATE ***********
**********************************************

use "$dir/data/analysis_main.dta", clear

	
keep if pid1==1
keep pid blhrprod blhrprodsq blhrprod_miss blatt blattsq blatt_miss ///
	educ educsq married widowed divorced exper expersq wealth_hi_avg wealth_ind_avg hasnocredits hasloan loan_moneylender anyloans_worry richhouse richhouse_miss canget1k canget1k_miss ownland ownland_miss age_nm age_miss read_odiya_nm read_odiya_miss anychildren_nm children_miss caslabor_nm caslabor_miss bs_labor_7_nm bs_labor_7_miss bs_labor_30_nm bs_labor_30_miss future_fin_worry_nm worry1_nm worry2_nm future_fin_worry_miss bl_loanamt_nm bl_loanamt_miss anyloans_dueyr0_nm numloans_dueyr0_nm numloans_dueyr0_miss

sort pid
tempfile mainround
save `mainround', replace

use "$dir/Data/analysis_all.dta", clear
replace piecerate = 0 if day==3 & pid==438
drop if pid==431

foreach x in blhrprod blhrprodsq blhrprod_miss blatt blattsq blatt_miss educ educsq married widowed divorced exper expersq wealth_hi_avg wealth_ind_avg hasnocredits hasloan loan_moneylender anyloans_worry richhouse richhouse_miss canget1k canget1k_miss ownland ownland_miss age_nm age_miss read_odiya_nm read_odiya_miss anychildren_nm children_miss caslabor_nm caslabor_miss bs_labor_7_nm bs_labor_7_miss bs_labor_30_nm bs_labor_30_miss future_fin_worry_nm worry1_nm worry2_nm future_fin_worry_miss bl_loanamt2_nm bl_loanamt2_miss bl_loanamt_nm bl_loanamt_miss anyloans_dueyr0_nm numloans_dueyr0_nm numloans_dueyr0_miss{
	capture confirm variable `x'
	if _rc==0 drop `x'
}

keep if round > 15
sort pid, stable
merge m:1 pid using `mainround'
drop if _merge==2

cap drop pid1 pidday1
egen pid1=tag(pid) if attend==1 & day!=1
egen pidday1=tag(pid day) 


*** wage and piece rate
replace baserate=. if attend==0
gen wage = baserate + piecerate*day_output   // does not include bonuses and game payments
replace wage=. if attend==0
gen lwage = log(wage)

gen pr0=piecerate==0
gen pr2=piecerate==2
gen pr3=piecerate==3
gen pr4=piecerate==4

label var pr0 "Piece rate = Rs. 0"
label var pr2 "Piece rate = Rs. 2"
label var pr3 "Piece rate = Rs. 3"
label var pr4 "Piece rate = Rs. 4"

gen lpiecerate = log(piecerate)
label var lpiecerate "Log(piece rate)"
label var piecerate "Piece rate"



* One person dropped out at the beginning in the main rounds; no baseline info
gen baseline_miss = pid==455
foreach x of varlist blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss blatt blatt_miss read_odiya_nm read_odiya_miss divorced{
	replace `x' = 0 if baseline_miss==1
}

* one person left early on one day
gen attend2 = attend
replace attend2 = 1 if pid==17 & day==4 & slot==5

* conditional on attendance
eststo clear 
	eststo: reghdfe n_productivity piecerate blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss baseline_miss if day>1 & piecerate>0 & attend2==1, vce(cl pid) absorb(round ) 
	eststo: reghdfe n_productivity lpiecerate blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss baseline_miss if day>1 & piecerate>0 & attend2==1, vce(cl pid) absorb(round ) 
	eststo: reghdfe n_productivity pr3 pr4 blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss baseline_miss if day>1 & piecerate>0 & attend2==1, vce(cl pid) absorb(round ) 
	
	eststo: reghdfe att_ind piecerate blatt blatt_miss blhrprod_miss read_odiya_nm read_odiya_miss bs_labor_30_miss future_fin_worry_miss baseline_miss if day>1 & piecerate>0 & attend2==1, vce(cl pid) absorb(round ) 
	eststo: reghdfe att_ind lpiecerate blatt blatt_miss blhrprod_miss read_odiya_nm read_odiya_miss bs_labor_30_miss future_fin_worry_miss baseline_miss if day>1 & piecerate>0 & attend2==1, vce(cl pid) absorb(round ) 
	eststo: reghdfe att_ind pr3 pr4 blatt blatt_miss blhrprod_miss read_odiya_nm read_odiya_miss bs_labor_30_miss future_fin_worry_miss baseline_miss if day>1 & piecerate>0 & attend2==1, vce(cl pid) absorb(round ) 

	eststo: reghdfe attend2 piecerate blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss baseline_miss if day>1 & piecerate>0 & pidday1==1, vce(cl pid) absorb(round ) 
	eststo: reghdfe attend2 lpiecerate blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss baseline_miss if day>1 & piecerate>0 & pidday1==1, vce(cl pid) absorb(round ) 
	eststo: reghdfe attend2 pr3 pr4 blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss baseline_miss if day>1 & piecerate>0 & pidday1==1, vce(cl pid) absorb(round ) 

#delimit ;
local tablerow piecerate lpiecerate pr3 pr4;

esttab using "$om/$t/Table_5.tex", 
	b(3) se booktabs star(* .1 ** .05 *** .01) nonotes nomtitles  
	replace label style(tex) gaps keep(`tablerow') order(`tablerow') 
	stats(N, labels("N: worker-hours") fmt(%9.0fc)) 
	prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}
	{@{\extracolsep{4pt}}l*{10}{>{\centering\arraybackslash}m{1.6cm}}@{}} 
	\toprule 
	&\multicolumn{3}{c}{\textbf{Hourly production}}  &\multicolumn{3}{c}{\textbf{Attentiveness index}}  &\multicolumn{3}{c}{\textbf{Attendance}} \bigstrut       \\
	\cline{2-4} \cline{5-7} \cline{8-10} \addlinespace 
	") ;
#delimit cr	

	// Testing equality of coefficients across the columns above

	keep round pid day slot n_productivity att_ind attend2 piecerate lpiecerate pr3 pr4 wealth_hi_avg blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss baseline_miss blatt blatt_miss read_odiya_nm read_odiya_miss
	keep if attend2==1 
	tempfile toappend
	save `toappend'

	gen depvar = n_productivity
	gen att = 0

	append using `toappend'
	replace att = 1 if mi(depvar)
	replace depvar = att_ind if att==1

	foreach x of varlist piecerate lpiecerate pr3 pr4 {
		gen `x'_att = `x'*att
	}
	foreach x of varlist blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss baseline_miss wealth_hi_avg{
		gen `x'_v1 = `x' if att==0
		replace `x'_v1 = 0 if att==1
	}

	foreach x of varlist blatt blatt_miss blhrprod_miss read_odiya_nm read_odiya_miss bs_labor_30_miss future_fin_worry_miss baseline_miss wealth_hi_avg{
		gen `x'_v2 = `x' if att==1
		replace `x'_v2 = 0 if att==0
	}

	egen pidatt = group(pid att)
	egen roundatt = group(round att)

	reghdfe depvar piecerate piecerate_att blhrprod_v1 blhrprodsq_v1 blhrprod_miss_v1 bs_labor_30_miss_v1 future_fin_worry_miss_v1 baseline_miss_v1 blatt_v2 blatt_miss_v2 blhrprod_miss_v2 read_odiya_nm_v2 read_odiya_miss_v2 bs_labor_30_miss_v2 future_fin_worry_miss_v2 baseline_miss_v2 if day>1 & piecerate>0 & attend2==1, vce(cl pid) absorb(roundatt)
	lincom piecerate_att
	local pval1 =  round(2*ttail(r(df),abs(r(estimate)/r(se))),.001)
	di "`pval1'"
		// pval piecerate_att: 0.001
	reghdfe depvar lpiecerate lpiecerate_att blhrprod_v1 blhrprodsq_v1 blhrprod_miss_v1 bs_labor_30_miss_v1 future_fin_worry_miss_v1 baseline_miss_v1 blatt_v2 blatt_miss_v2 blhrprod_miss_v2 read_odiya_nm_v2 read_odiya_miss_v2 bs_labor_30_miss_v2 future_fin_worry_miss_v2 baseline_miss_v2 if day>1 & piecerate>0 & attend2==1, vce(cl pid) absorb(roundatt)
		// pval piecerate_att: 0.001
		lincom lpiecerate_att
		local pval2 =  round(2*ttail(r(df),abs(r(estimate)/r(se))),.001)
		di "`pval2'"
		// pval lpiecerate_att: 0.001
	reghdfe depvar pr3 pr4 pr3_att pr4_att blhrprod_v1 blhrprodsq_v1 blhrprod_miss_v1 bs_labor_30_miss_v1 future_fin_worry_miss_v1 baseline_miss_v1 blatt_v2 blatt_miss_v2 blhrprod_miss_v2 read_odiya_nm_v2 read_odiya_miss_v2 bs_labor_30_miss_v2 future_fin_worry_miss_v2 baseline_miss_v2 if day>1 & piecerate>0 & attend2==1, vce(cl pid) absorb(roundatt)
		lincom pr3_att
		local pval3 =  round(2*ttail(r(df),abs(r(estimate)/r(se))),.001)
		di "`pval3'"
		
		lincom pr4_att
		local pval4 =  round(2*ttail(r(df),abs(r(estimate)/r(se))),.001)
		di "`pval4'"
		// pval pr3_att: 0.211
		// pval pr4_att: 0.001		

		file open f using "$om/$t/Table_5.tex", read
		local liner ""
		forvalues x = 1/17{
		file read f line
		local liner = "`liner' `line'"
		}
		file close f
		file open f using "$om/$t/Table_5.tex", write replace
		file write f "`liner'"
		file close f
		
		file open f using "$om/$t/Table_5.tex", write append
		file write f _n "P-val: equality of coefficients &&&&&&&&& \\" _n "Piece rate in (1) and (4) & `pval1' &&&&&&&& \\" _n "Log(piece rate) in (2) and (5) && `pval2' &&&&&&& \\" _n "Piece rate = Rs. 3 in (3) and (6) &&& `pval3' &&&&&& \\" _n "Piece rate = Rs. 4 in (3) and (6) &&& `pval4' &&&&&& \\" _n "\bottomrule" _n "\end{tabular}" _n "}"
		file close f
		
		
		
		
